/*
 * Copyright (c) 2023, LXMS (lxinet.cn).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.lxinet.lxms.framework.core.invoke;

import cn.lxinet.lxms.framework.core.annotation.Plugin;
import cn.lxinet.lxms.framework.core.exception.LxmsException;
import cn.lxinet.lxms.framework.core.exception.NotAuthorizeException;
import cn.lxinet.lxms.framework.core.exception.NotLoginException;
import cn.lxinet.lxms.framework.core.exception.OpeErrorException;
import cn.lxinet.lxms.framework.core.utils.SpringContextUtil;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.target.SingletonTargetSource;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author zcx
 * @date 2023/07/01
 */
public class LxmsInvoke {

    public static Object invoke(String className, String methodName, Object...params){
        try {
            Object bean = SpringContextUtil.getBean(className);
            Method method = getMethod(bean, methodName, params);
            return method.invoke(bean, params);
        }catch (NotLoginException ne){
            throw new NotLoginException();
        }catch (NoSuchBeanDefinitionException e1){
            throw new NotAuthorizeException();
        }catch (NoSuchMethodException e2){
            throw new NotAuthorizeException();
        }catch (Exception e){
            e.printStackTrace();
            String msg = e.getMessage();
            if (e instanceof InvocationTargetException){
                msg = ((InvocationTargetException) e).getTargetException().getMessage();
            }
            throw new LxmsException(msg);
        }
    }

    private static Method getMethod(Object bean, String methodName, Object...params) throws NoSuchMethodException {
        Class clazz = bean.getClass();
        if (clazz.toString().startsWith("class com.sun.proxy")){
            bean = ((SingletonTargetSource)((Advised) bean).getTargetSource()).getTarget();
            clazz = bean.getClass();
        }
        validate(clazz);
        Class<?>[] paramsTypes = new Class[params.length];
        for (int i = 0; i < params.length; i ++){
            paramsTypes[i] = params[i].getClass();
        }
        Method method = clazz.getMethod(methodName, paramsTypes);
        return method;
    }

    private static void validate(Class clazz){
        Annotation plugin = clazz.getAnnotation(Plugin.class);
        //只有使用了@Plugin注解的方法才可以使用
        if (null == plugin){
            throw new OpeErrorException("非法访问");
        }
    }
}
